package com.sxj.corejava.code09_集合.List;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author 石小俊
 * 提供5名学生信息,包含学号(1-5)和成绩,创建Student对象(属性:no、score),然后放到ArrayList集合中
 * 1.遍历集合,删除学号为2的学生
 * 2.不遍历集合,删除学号为3的学生
 * 3.不遍历集合,判断集合中是否包含学号为5的学生
 */
public class Test06_练习 {

    public static void main(String[] args) {
        // 提供5名学生信息,包含学号(1-5)和成绩,创建Student对象(属性:no、score),然后放到ArrayList集合中
        List<Student> students = new ArrayList<>(Arrays.asList(
                new Student("1", 80),
                new Student("2", 90),
                new Student("3", 100),
                new Student("4", 88),
                new Student("5", 85)
        ));
        System.out.println(students);
        // 1.遍历集合,删除学号为2的学生
        for (int i = students.size() - 1; i >= 0; i--) {
            // 取出当前迭代的学生对象
            Student student = students.get(i);
            if ("2".equals(student.getNo())) {
                students.remove(i);
            }
        }
        System.out.println(students);

        // 2.不遍历集合,删除学号为3的学生
        Student student3 = new Student();
        student3.setNo("3");
        students.remove(student3);
        System.out.println(students);

        // 3.不遍历集合,判断集合中是否包含学号为5的学生
        Student student5 = new Student();
        student5.setNo("5");
        System.out.println("集合中是否包含学号为5的学生:" + students.contains(student5));

    }

}

class Student {
    private String no;
    private double score;

    @Override
    public String toString() {
        return "Student{" +
                "no='" + no + '\'' +
                ", score=" + score +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Student student = (Student) o;
        return Objects.equals(no, student.no);
    }

    @Override
    public int hashCode() {
        return Objects.hash(no);
    }

    public Student() {
    }

    public Student(String no, double score) {
        this.no = no;
        this.score = score;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}